package com.sugon.modules.analyse.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.dao.SysAvailabilityAnalyseDao;
import com.sugon.entity.SysUserEntity;
import com.sugon.modules.analyse.model.vo.SysDeptAvaiStatVO;
import com.sugon.modules.analyse.model.vo.SysUserAvaiStatVO;
import com.sugon.modules.analyse.service.ISysAvailabilityAnalyseService;
import com.sugon.service.SysUserService;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description: 磨具分析统计Service
 * @author: YuXD
 * @create: 2020-05-10 19:56
 **/
@Slf4j
@Service
public class SysAvailabilityAnalyseServiceImpl implements ISysAvailabilityAnalyseService {

    @Autowired
    private SysAvailabilityAnalyseDao sysAvaiStatDao;
    @Autowired
    private SysUserService userService;

    /**
     * statDeptAvailability
     *
     * @param params 包含分页和业务查询字段信息
     * @return
     */
    @Override
    public SysDeptAvaiStatVO statDeptAvailability(Map<String, Object> params) {
        SysDeptAvaiStatVO deptAvaiStatVO = new SysDeptAvaiStatVO();
        // 对参数进行处理
        checkQueryParam(params);
        // 执行查询
        List<Map<String, Object>> deptAvaiStatList = sysAvaiStatDao.statDeptAvai(params);
        if (CollectionUtil.isNotEmpty(deptAvaiStatList)) {
            for (Map<String, Object> deptAvaiStat : deptAvaiStatList) {
                deptAvaiStatVO.getXAxisData().add(MapUtil.getStr(deptAvaiStat, "DEPTNAME"));
                deptAvaiStatVO.getSeriesData().add(MapUtil.getInt(deptAvaiStat, "TIMES"));
            }
        }
        return deptAvaiStatVO;
    }

    /**
     * 校验查询参数
     *
     * @param params
     */
    private void checkQueryParam(Map<String, Object> params) {
        // 如果beginTime为空，则直接从参数中移除
        String beginTime = MapUtil.getStr(params, "beginTime");
        if (StrUtil.isEmpty(beginTime)) {
            params.remove("beginTime");
        }
        // 如果endTime为空，则直接从参数中移除
        String endTime = MapUtil.getStr(params, "endTime");
        if (StrUtil.isEmpty(endTime)) {
            params.remove("endTime");
        }
        // 如果curDeptId为空，在参数中添加curDeptId
        String curDeptId = MapUtil.getStr(params, "curDeptId");
        if (StrUtil.isEmpty(curDeptId)) {
            params.put("curDeptId", "01");
        }
    }

    /**
     * statUserAvailability
     *
     * @param params 包含分页和业务查询字段信息
     * @return
     */
    @Override
    public PageUtils statUserAvailability(Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        // 1、查询出分页用户列表,如果为空则直接返回
        List<SysUserEntity> userList = userService.queryList(query);
        if (CollectionUtil.isEmpty(userList)) {
            return new PageUtils(new ArrayList<>(), 0, query.getLimit(), query.getPage());
        }
        // 2、获取用户名列表并加入到插叙参数
        List<String> unameList = userList.stream().map(SysUserEntity::getUserName).collect(Collectors.toList());
        query.put("unameList", unameList);
        // 3、执行查询并处理参数
        List<Map<String, Object>> sysUserAvaiStatList = sysAvaiStatDao.statUserAvai(query);
        Map<String, Integer> sysUserAvaiStat = new HashMap<>(sysUserAvaiStatList.size());
        for (Map<String, Object> sysUserAvaiStatMap : sysUserAvaiStatList) {
            sysUserAvaiStat.put(MapUtil.getStr(sysUserAvaiStatMap, "USERNAME"), MapUtil.getInt(sysUserAvaiStatMap, "TIMES"));
        }
        // 4、
        List<SysUserAvaiStatVO> avaiStatList = new ArrayList<>();
        SysUserAvaiStatVO avaiStat;
        for (SysUserEntity user : userList) {
            avaiStat = new SysUserAvaiStatVO();
            avaiStat.setUsername(user.getUserName());
            avaiStat.setDeptName(user.getDeptName());
            avaiStat.setPost(user.getNickName());
            avaiStat.setTimes(sysUserAvaiStat.get(user.getUserName()));
            avaiStatList.add(avaiStat);
        }
        // 获取总记录数
        int total = userService.queryTotal(query);
        return new PageUtils(avaiStatList, total, query.getLimit(), query.getPage());
    }

}
